package com.example.pongv1;



import java.io.IOException;

import javax.microedition.khronos.opengles.GL10;

import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.TextMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ColorMenuItemDecorator;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;
import org.andengine.util.debug.Debug;

import com.example.pongv1.Constantes.LEVEL;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.view.KeyEvent;
import android.widget.EditText;

public class MainActivity extends BaseGameActivity {

	private final float					CWIDTH		= 800;
	private final float					CHEIGHT		= 480;

	private Camera						camera;
	private Scene						mainScene;
	private MenuScene					menuScene;
	private final int					MENU_PLAY	= 0;
	private final int					MENU_OPT	= 1;

	private BuildableBitmapTextureAtlas	menuBtnTex;
	private ITextureRegion				menuBtnPlayReg;
	private ITextureRegion				menuBtnOptionsReg;
	private final int bnom_id = 13;
	
	

private RessourcesManager ressourcesManager;
	
	@Override
	public EngineOptions onCreateEngineOptions() {
		camera = new Camera(0, 0,CWIDTH , CHEIGHT);
		this.camera.setCenter(0,0);
	
	    EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(800, 480), this.camera);
	    engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
	    engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
	    
	    
	    return engineOptions;
	}
	@Override
	public Engine onCreateEngine(EngineOptions pEngineOptions) {
		return new LimitedFPSEngine(pEngineOptions, 60);
	}

	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		RessourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
	    ressourcesManager = RessourcesManager.getInstance();
	

	    pOnCreateResourcesCallback.onCreateResourcesFinished();
	   
	    // Set player name from DB.
	    SharedPreferences settings = getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	       String pname = settings.getString("playername", "ooops");
	       ressourcesManager.playername=pname;
	       //set IAlevel
	       int dbialevel=settings.getInt("IALEVEL", 1);
	       switch(dbialevel){
	       case 1:
	       ressourcesManager.IAlevel=LEVEL.IALEVEL1;
	       break;
	       case 2:
		       ressourcesManager.IAlevel=LEVEL.IALEVEL2;
		       break;
	       case 3:
		       ressourcesManager.IAlevel=LEVEL.IALEVEL3;
		       break;
	       case 4:
		       ressourcesManager.IAlevel=LEVEL.IALEVEL4;
		       break;
	       case 5:
		       ressourcesManager.IAlevel=LEVEL.IALEVEL5;
		       break;
	       case 6:
		       ressourcesManager.IAlevel=LEVEL.IALEVEL6;
		       break;
	       case 7:
		       ressourcesManager.IAlevel=LEVEL.IALEVEL7;
		       break;
	       case 8:
		       ressourcesManager.IAlevel=LEVEL.IALEVEL8;
		       break;
	       case 9:
		       ressourcesManager.IAlevel=LEVEL.IALEVEL9;
		       break;
	       case 10:
		       ressourcesManager.IAlevel=LEVEL.IALEVEL10;
		       break;
	       default:
			   ressourcesManager.IAlevel=LEVEL.IALEVEL1;
			   break;
	       }
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		// TODO Auto-generated method stub
		 SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);

				
				
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		// TODO Auto-generated method stub
		 mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback() 
		    {
		            public void onTimePassed(final TimerHandler pTimerHandler) 
		            {
		               
		                mEngine.unregisterUpdateHandler(pTimerHandler);
		                SceneManager.getInstance().createMenuScene();      
		            }
		    }));
		    pOnPopulateSceneCallback.onPopulateSceneFinished();
		
	}


	@Override
	protected void onDestroy()
	{
	    super.onDestroy();
	        
	    if (this.isGameLoaded())
	    {
	        System.exit(0);    
	    }
	    
	}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{  
    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
     
    }
    return false; 
}

@Override
protected Dialog onCreateDialog(final int pID) {
	switch(pID) {
		
		case bnom_id:
		
	        	final EditText nameEditText = new EditText(this);
				 return new AlertDialog.Builder(this)
			
				.setTitle("Enter your name ...")
				.setCancelable(false)
				.setView(nameEditText)
				.setPositiveButton("OK", new OnClickListener() {
					@Override
					public void onClick(final DialogInterface pDialog, final int pWhich) {
						ressourcesManager.playername =nameEditText.getText().toString();
						 SceneManager.getInstance().loadOptionScene();
						 
						 //set player name in data base
						 SharedPreferences settings = getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
			               SharedPreferences.Editor editor = settings.edit();
			               editor.putString("playername", ressourcesManager.playername);
			               editor.commit();
						
					}
				})
				.setNegativeButton("cancel", new OnClickListener() {
					@Override
					public void onClick(final DialogInterface pDialog, final int pWhich) {
						
					}
				})
				.create();
		default:
			return super.onCreateDialog(pID);
	}
	
}

}
